#include<iostream>
using namespace std;


namespace xywl
{
    // List的节点类
    template<class T>
    struct ListNode
    {
        ListNode(const T& val = T()) 
        :_val(val), _pPre(nullptr), _pNext(nullptr) {}
        ListNode<T>* _pPre;
        ListNode<T>* _pNext;
        T _val;
    };


    //List的迭代器类
    template<class T, class Ref, class Ptr>
    struct ListIterator
    {
        typedef ListNode<T>* PNode;
        typedef ListIterator<T, Ref, Ptr> Self;
        ListIterator(PNode pNode = nullptr) 
        :_pNode(pNode) {}
        ListIterator(const Self& l) 
        :_pNode(l._pNode) {}
        T& operator*() {
            return _pNode->_val;
        }
        T* operator->() {
            return &_pNode->_val;
        }
        Self& operator++() { // 前置++
            _pNode = _pNode->_pNext;
            return *this;
        }
        Self operator++(int) {
            Self temp(*this);
            _pNode = _pNode->_pNext;
            return temp;
        }
        Self& operator--() {
            _pNode = _pNode->_pPre;
            return *this;
        }
        Self& operator--(int) {
            Self temp(*this);
            _pNode = _pNode->_pPre;
            return temp;
        }
        bool operator!=(const Self& l) {
            return _pNode != l._pNode;
        }
        bool operator==(const Self& l) {
            return _pNode == l._pNode;
        }
        PNode _pNode;
    };


    //list类
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;
    public:
        typedef ListIterator<T, T&, T*> iterator;
        typedef ListIterator<T, const T&, const T&> const_iterator;
    public:
        ///////////////////////////////////////////////////////////////
        // List的构造
        list() {
            CreateHead();
        }
        list(int n, const T& value = T()) {
            int size = n;
            CreateHead();
            while(size--) {
                push_back(value);
            }
        }
        template <class Iterator>
        list(Iterator first, Iterator last) {
            CreateHead();
            while(first != last) {
                push_back(*first);
                ++first;
            }
        }
        list(const list<T>& l) {
            CreateHead();
            for(auto it = l.begin(); it != l.end(); it++) {
                push_back(*it);
            }
        }
        list<T>& operator=(const list<T> l) {
            if(this != &l) {
                clear();
                for(auto it = l.begin(); it != l.end(); it++) {
                    push_back(*it);
                }
            }
            return *this;
        }
        ~list() {
            clear();
            delete _pHead;
            _pHead = nullptr;
        }


        ///////////////////////////////////////////////////////////////
        // List Iterator
        iterator begin() {
            return iterator(_pHead->_pNext);
        }
        iterator end() {
            return iterator(_pHead);
        }
        const_iterator begin() const {
            return const_iterator(_pHead->_pNext);
        }
        const_iterator end() const {
            return const_iterator(_pHead);
        }


        ///////////////////////////////////////////////////////////////
        // List Capacity
        size_t size()const {
            size_t count = 0;
            PNode temp = _pHead->_pNext;
            while(temp != _pHead) {
                count++;
                temp = temp->_pNext;
            }
            return count;
        }
        bool empty()const {
            return _pHead->_pNext == _pHead;
        }


        ////////////////////////////////////////////////////////////
        // List Access
        T& front() {
            return _pHead->_pNext->_val;
        }
        const T& front()const {
            return _pHead->_pNext->_val;
        }
        T& back() {
            return _pHead->_pPre->_val;
        }
        const T& back()const {
            _pHead->_pPre->_val;
        }


        ////////////////////////////////////////////////////////////
        // List Modify
        void push_back(const T& val) { insert(end(), val); }
        void pop_back() { erase(--end()); }
        void push_front(const T& val) { insert(begin(), val); }
        void pop_front() { erase(begin()); }
        // 在pos位置前插入值为val的节点
        iterator insert(iterator pos, const T& val) {
            PNode temp = pos._pNode;
            PNode pre = temp->_pPre;
            PNode newnode = new Node(val);
            newnode->_pNext = temp;
            pre->_pNext = newnode;
            temp->_pPre = newnode;
            newnode->_pPre = pre;
            return iterator(newnode);
        }
        // 删除pos位置的节点，返回该节点的下一个位置
        iterator erase(iterator pos) {
            PNode temp = pos._pNode;
            if(temp == _pHead) {
                return pos;
            }
            PNode pre = temp->_pPre;
            PNode next = temp->_pNext;
            pre->_pNext = next;
            next->_pPre = pre;
            delete temp;
            return iterator(next);
        }
        void clear() {
            PNode temp = _pHead->_pNext;
            while(temp != _pHead) {
                PNode next = temp->_pNext;
                delete temp;
                temp = next;
            }
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;
        }
        void swap(list<T>& l) {
            std::swap(_pHead, l._pHead);
        }
    private:
        void CreateHead() {
            _pHead = new Node;
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;
        }
        PNode _pHead;
    };
};

int main()
{
    xywl::list<int> l;
    l.push_back(1);
    l.push_back(2);
    l.push_back(3);
    l.push_front(0);

    cout << "List: ";
    for (auto it = l.begin(); it != l.end(); ++it)
        cout << *it << " ";
    cout << endl;

    l.pop_front();
    l.pop_back();

    cout << "After pop: ";
    for (auto it = l.begin(); it != l.end(); ++it)
        cout << *it << " ";
    cout << endl;

    cout << "Front: " << l.front() << ", Back: " << l.back() << endl;
    cout << "Size: " << l.size() << endl;

    return 0;
}